<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006 Russell Peter Phillips

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include ('../inc/inc_head_db.php');
include ('../inc/inc_admin.php');
include ('../inc/inc_head_html.php');

$db_prefix = DB_PREFIX;
$key = CRYPT_KEY;

if ($_GET ['action'] == 'remove') {
	//Set up UPDATE query to remove bunk
	$pid = (int) $_GET ['pid'];
	$sql_update = "UPDATE {$db_prefix}players SET plBunkAssigned = 0 WHERE plPlayerID = $pid";
	ba_db_query ($link, $sql_update);
}

foreach ($_POST as $chk) {
	if ((int) $chk > 0) {
		//Box is ticked. Set up UPDATE query to assign bunk
		$iPlayerID = (int) $chk;
		$sql_update = "UPDATE {$db_prefix}players SET plBunkAssigned = 1 WHERE plPlayerID = " . $iPlayerID;
		ba_db_query ($link, $sql_update);
	}
}

//Get list of players that have paid and requested bunks
$sql = "SELECT plPlayerID, " . 
	"plFirstName, " .
	"plSurname, " .
	"plBookAs, " .
	"plBunkRequested, " .
	"plBunkAssigned, " .
	"AES_DECRYPT(pleMedicalInfo, '$key') AS dMedicalInfo, " .
	"chName, " .
	"bkDateOOCConfirmed, " .
	"bkDateICConfirmed, " .
	"bkDatePaymentConfirmed " .
	"FROM {$db_prefix}players, {$db_prefix}characters, {$db_prefix}bookings " .
	"WHERE plPlayerID = chPlayerID AND chPlayerID = bkPlayerID AND " .
	"bkDatePaymentConfirmed <> '0000-00-00' AND bkDatePaymentConfirmed <> ''";
$result = ba_db_query ($link, $sql);
?>
<script src="../inc/sorttable.js" type="text/javascript"></script>

<h1><?php echo TITLE?> - Bunks</h1>

<p>
<a href = 'admin.php'>Admin</a>
</p>

<p>
The following people have paid. Click on a column header to sort by that column. To assign bunks, tick the relevant players boxes and click Submit. To remove a bunk assignment, click on the <b>Remove</b> link.
</p>

<form action = 'admin_bunks.php' method = 'post'>

<table border = '1' class="sortable">
<tr>
<th>Assign Bunk</th>
<th>Remove Bunk</th>
<th>Player ID</th>
<th>OOC Name</th>
<th>IC Name</th>
<th>Medical Info</th>
<th>Booking As</th>
<th>Date OOC Confirmed</th>
<th>Date IC Confirmed</th>
<th>Date Payment Confirmed</th>
<th>Bunk Requested?</th>
</tr>

<?php
while ($row = ba_db_fetch_assoc ($result)) {
	echo "<tr class = 'highlight'><td class = 'mid'>";
	if ($row ['plBunkAssigned'] == 1)
		$sChecked = ' checked';
	else
		$sChecked = '';
	echo "<input type = 'checkbox' name = 'chkPl{$row ['plPlayerID']}' value = '{$row ['plPlayerID']}'{$sChecked}></td>\n";
	echo "<td><a href = 'admin_bunks.php?action=remove&amp;pid={$row ['plPlayerID']}'>Remove</a></td>\n";
	echo "<td>" . PID_PREFIX . sprintf ('%03s', $row ['plPlayerID']) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['plFirstName'])) . " " . htmlentities (stripslashes ($row ['plSurname'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['chName'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['dMedicalInfo'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['plBookAs'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['bkDateOOCConfirmed'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['bkDateICConfirmed'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['bkDatePaymentConfirmed'])) . "</td>\n";
	if ($row ['plBunkRequested'] == 0)
		echo "<td>No</td></tr>\n";
	else
		echo "<td>Yes</td></tr>\n";
}
?>

</table>

<p>
<input type = 'submit' value = 'Submit' name = 'btnSubmit'>&nbsp;
<input type = 'reset' value = 'Reset'>
</p>
</form>

<?php
include ('../inc/inc_foot.php');
?>
